/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// DlgBldWOL.cpp : implementation file
//

#include "stdafx.h"
#include "CHScanner.h"
#include "DlgBldWOL.h"

//---------------------------------------------------------------------------

IMPLEMENT_DYNAMIC(DlgBldWOL, CRgnDialog)

DlgBldWOL::DlgBldWOL(CWnd* pParent /*=NULL*/) : CRgnDialog(DlgBldWOL::IDD, pParent)
{
	skinIn = NULL;
	retVal = false;
	dlgResult.Empty();
}

DlgBldWOL::~DlgBldWOL()
{
}

//---------------------------------------------------------------------------

void DlgBldWOL::DoDataExchange(CDataExchange* pDX)
{
	CRgnDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BTN_OK, btnOk);
	DDX_Control(pDX, IDC_BTN_CANCEL, btnCancel);
	DDX_Control(pDX, IDC_EDT_PORT, edtPort);
	DDX_Control(pDX, IDC_RD_UDP1, rUdp1);
	DDX_Control(pDX, IDC_RD_UDP2, rUdp2);
	DDX_Control(pDX, IDC_RD_UDP3, rUdp3);
	DDX_Control(pDX, IDC_RD_ICMP, rIcmp);
	DDX_Control(pDX, IDC_RD_UDP4, rUdp4);
}

//---------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(DlgBldWOL, CRgnDialog)
	ON_BN_CLICKED(IDC_BTN_OK, &DlgBldWOL::OnBnClickedBtnOk)
	ON_BN_CLICKED(IDC_BTN_CANCEL, &DlgBldWOL::OnBnClickedBtnCancel)
	ON_BN_CLICKED(IDC_RD_UDP1, &DlgBldWOL::OnBnClickedRdUdp1)
	ON_BN_CLICKED(IDC_RD_UDP2, &DlgBldWOL::OnBnClickedRdUdp2)
	ON_BN_CLICKED(IDC_RD_UDP3, &DlgBldWOL::OnBnClickedRdUdp3)
	ON_BN_CLICKED(IDC_RD_ICMP, &DlgBldWOL::OnBnClickedRdIcmp)
	ON_BN_CLICKED(IDC_RD_UDP4, &DlgBldWOL::OnBnClickedRdUdp4)
END_MESSAGE_MAP()

//---------------------------------------------------------------------------

BOOL DlgBldWOL::OnInitDialog()
{
	CRgnDialog::OnInitDialog();

	SetProperties(skinIn, false, _T("WMI"));

	if(skin!=NULL){
		rUdp1.textColor = skin->textColor;
		rUdp2.textColor = skin->textColor;
		rUdp3.textColor = skin->textColor;
		rIcmp.textColor = skin->textColor;
		rUdp4.textColor = skin->textColor;
	}

	WorkCfgToData();

	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
}

//---------------------------------------------------------------------------

void DlgBldWOL::OnOK()
{
	// CRgnDialog::OnOK();
}

void DlgBldWOL::OnCancel()
{
	// CRgnDialog::OnCancel();
}

//---------------------------------------------------------------------------

void DlgBldWOL::OnBnClickedBtnOk()
{
	UpdateData(TRUE);
	WorkCfgFromData();

	retVal = true;
	EndDialog(IDOK);
}

void DlgBldWOL::OnBnClickedBtnCancel()
{
	retVal = false;
	EndDialog(IDABORT);
}

//---------------------------------------------------------------------------

void DlgBldWOL::OnBnClickedRdUdp1()
{
	if(BST_CHECKED==rUdp1.GetCheck()){
		rUdp2.SetCheck(BST_UNCHECKED);
		rUdp3.SetCheck(BST_UNCHECKED);
		rUdp4.SetCheck(BST_UNCHECKED);
		rIcmp.SetCheck(BST_UNCHECKED);
		edtPort.EnableWindow(FALSE);
	}
}

void DlgBldWOL::OnBnClickedRdUdp2()
{
	if(BST_CHECKED==rUdp2.GetCheck()){
		rUdp1.SetCheck(BST_UNCHECKED);
		rUdp3.SetCheck(BST_UNCHECKED);
		rUdp4.SetCheck(BST_UNCHECKED);
		rIcmp.SetCheck(BST_UNCHECKED);
		edtPort.EnableWindow(FALSE);
	}
}

void DlgBldWOL::OnBnClickedRdUdp3()
{
	if(BST_CHECKED==rUdp3.GetCheck()){
		rUdp1.SetCheck(BST_UNCHECKED);
		rUdp2.SetCheck(BST_UNCHECKED);
		rUdp4.SetCheck(BST_UNCHECKED);
		rIcmp.SetCheck(BST_UNCHECKED);
		edtPort.EnableWindow(FALSE);
	}
}

void DlgBldWOL::OnBnClickedRdIcmp()
{
	if(BST_CHECKED==rIcmp.GetCheck()){
		rUdp1.SetCheck(BST_UNCHECKED);
		rUdp2.SetCheck(BST_UNCHECKED);
		rUdp3.SetCheck(BST_UNCHECKED);
		rUdp4.SetCheck(BST_UNCHECKED);
		edtPort.EnableWindow(FALSE);
	}
}

void DlgBldWOL::OnBnClickedRdUdp4()
{
	if(BST_CHECKED==rUdp4.GetCheck()){
		rUdp1.SetCheck(BST_UNCHECKED);
		rUdp2.SetCheck(BST_UNCHECKED);
		rUdp3.SetCheck(BST_UNCHECKED);
		rIcmp.SetCheck(BST_UNCHECKED);
		edtPort.EnableWindow(TRUE);
	}
}

//---------------------------------------------------------------------------

void DlgBldWOL::WorkCfgToData(void)
{
	CString str, sPort;
	int val;

	edtPort.EnableWindow(FALSE);

	str = dlgResult.Trim();
	if(str.IsEmpty()){
		rUdp1.SetCheck(BST_CHECKED);
		edtPort.SetWindowText(_T(""));
		return;
	}

	if(str.GetLength()>1) sPort = str.Mid(1);
	else                  sPort = _T("40000");

	val = _tstoi(sPort);
	if(val<1 || val>65535) val = 40000;
	sPort.Format(_T("%d"), val);

	str = str.Left(1);

	if(str.CompareNoCase(_T("D"))==0){
		rUdp2.SetCheck(BST_CHECKED);
		edtPort.SetWindowText(_T(""));
		return;
	}

	if(str.CompareNoCase(_T("N"))==0){
		rUdp3.SetCheck(BST_CHECKED);
		edtPort.SetWindowText(_T(""));
		return;
	}

	if(str.CompareNoCase(_T("U"))==0){
		rUdp4.SetCheck(BST_CHECKED);
		edtPort.SetWindowText(sPort);
		edtPort.EnableWindow(TRUE);
		return;
	}

	if(str.CompareNoCase(_T("I"))==0){
		rIcmp.SetCheck(BST_CHECKED);
		edtPort.SetWindowText(_T(""));
		return;
	}

	rUdp1.SetCheck(BST_CHECKED);
	edtPort.SetWindowText(_T(""));
}

void DlgBldWOL::WorkCfgFromData(void)
{
	CString str;
	int val;

	dlgResult.Empty();

	if(rUdp1.GetCheck()==BST_CHECKED) return;

	if(rUdp2.GetCheck()==BST_CHECKED){
		dlgResult = _T("D");
		return;
	}

	if(rUdp3.GetCheck()==BST_CHECKED){
		dlgResult = _T("N");
		return;
	}

	if(rUdp4.GetCheck()==BST_CHECKED){
		edtPort.GetWindowText(str);
		val = _tstoi(str);
		if(val<1 || val>65535) val = 40000;
		dlgResult.Format(_T("U%d"), val);
		return;
	}

	if(rIcmp.GetCheck()==BST_CHECKED){
		dlgResult = _T("I");
		return;
	}
}

//---------------------------------------------------------------------------

void DlgBldWOL::ResizeChilds(void)
{
	int bw, bh;		///< size of button controls
	int ew, eh;		///< size of edit and combobox controls
	int rw, rh, rwe;///< size of radio buttons
	int ml;			///< to vertical-align radion buttons with edit controls
	int tw, th;		///< total required width and hight
	int cw, ch;		///< client region's width and height
	int mx, my;		///< left and top space for displaying the objects in the center of the window
	int dd;			///< space between objects
	RECT rect;
	int posx1, posx2, posy;

	dd = 2;

	btnOk.GetWindowRect(&rect);
	bw = rect.right - rect.left;
	bh = rect.bottom - rect.top;

	edtPort.GetWindowRect(&rect);
	ew = rect.right - rect.left;
	eh = rect.bottom - rect.top;

	rUdp1.GetWindowRect(&rect);
	rw = rect.right - rect.left;
	rh = rect.bottom - rect.top;

	rUdp4.GetWindowRect(&rect);
	rwe = rect.right - rect.left;

	ml = eh - rh;
	ml /= 2;

	tw = dd + rw + dd;
	th = dd + ml + 5*(eh+dd) + bh + dd;

	cw = clientRect.right - clientRect.left;
	ch = clientRect.bottom - clientRect.top;

	mx = cw - tw; mx /= 2;
	my = ch - th; my /= 2;

	posx1 = clientRect.left + dd + mx;
	posx2 = posx1 + dd + rwe;
	posy  = clientRect.top + dd + my + ml;

	rUdp1.MoveWindow(posx1, posy, rw, rh, TRUE);
	posy += (eh + dd);
	rUdp2.MoveWindow(posx1, posy, rw, rh, TRUE);
	posy += (eh + dd);
	rUdp3.MoveWindow(posx1, posy, rw, rh, TRUE);
	posy += (eh + dd);
	rUdp4.MoveWindow(posx1, posy, rwe, rh, TRUE);
	edtPort.MoveWindow(posx2, posy - ml, ew, eh, TRUE);
	posy += (eh + dd);
	rIcmp.MoveWindow(posx1, posy, rw, rh, TRUE);
	posy += (eh + dd);

	posx2 = cw - bw - dd;
	posx1 = posx2 - bw - dd;

	btnOk.MoveWindow(posx1, posy, bw, bh, TRUE);
	btnCancel.MoveWindow(posx2, posy, bw, bh, TRUE);
}

bool DlgBldWOL::GetMinimumSize(RECT* rectIn)
{
	if(rectIn==NULL) return false;

	rectIn->left   = 0;
	rectIn->right  = 224;
	rectIn->top    = 0;
	rectIn->bottom = 159;

	return true;
}

//---------------------------------------------------------------------------
